﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ProductManage
{
    public partial class FormGetEverBlingProduct : Form
    {
        public FormGetEverBlingProduct()
        {
            InitializeComponent();
        }

        private void ReloadListUrl()
        {
            listCategory = this.richTextBox1.Text.Split('\n').ToList();
        }
        private int iPage = 1;
        private int iCa = 0;
        private List<string> listCategory = new List<string>(); 
        private void buttonRun_Click(object sender, EventArgs e)
        {
            ReloadListUrl();
            this.webBrowser1.DocumentCompleted -= webBrowser1_DocumentCompleted_LoadProduct;
            this.webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted_LoadProduct;
            Deal();

        }

        private void Deal()
        {
            var strUrl = listCategory[iCa];
            this.webBrowser1.Url = new Uri(strUrl + "?searchPage=" + iPage + "&searchRank=salesrank&searchSize=72");
        }
        List<string> list = new List<string>();
        void webBrowser1_DocumentCompleted_LoadProduct(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (webBrowser1.Document != null)
            {
                try
                {
                    var document = webBrowser1.Document;
                    var infos = document.GetElementById("searchResults");
                    try
                    {
                        if (infos == null)
                            return;
                        var index = 0;
                        while (infos.InnerHtml.IndexOf("<INPUT type=hidden value=", index + 1) > 0)
                        {
                            index = infos.InnerHtml.IndexOf("<INPUT type=hidden value=", index + 1);
                            var endIndex = infos.InnerHtml.IndexOf(" name=miniDetailAsin>", index);
                            var ASKU = infos.InnerHtml.Substring(index + 25, endIndex - index - 25);
                            if (!list.Contains(ASKU))
                            {
                                list.Add(ASKU);
                                this.richTextBox2.AppendText(ASKU+'\n');
                            }
                        }
                    }
                    catch
                    {
                    }
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message);
                }


            }
        }

        private void buttonNextCategory_Click(object sender, EventArgs e)
        {
            iCa++;
            iPage = 0;
            list.Clear();
            Deal();
        }

        private void buttonNextPage_Click(object sender, EventArgs e)
        {
            iPage++;
            Deal();
        }
    }
}
